package com.zcx.qshop.conn;

import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.AsyType;
import com.zcx.helper.http.note.HttpInlet;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/8/24 0024.
 */
@HttpInlet(Conn.INTEL_JSON_AREA)
public class JsonAreaAsyGet extends QSAsyGet<JsonAreaAsyGet.Info> {

    public String code;

    public JsonAreaAsyGet(String code, AsyCallBack<Info> asyCallBack) {

        super(asyCallBack);

        this.code = code;

        ASY_TYPE = AsyType.MAX_STALE;

    }

    @Override
    public Info parser(JSONObject object) {

        if(object.optString("message").equals("1")){

            Info info = new Info();

            JSONArray array1 = object.optJSONArray("ishotlist");

            if(array1 != null){

                for (int i = 0 ; i < array1.length() ; i ++){

                    JSONObject o1 = array1.optJSONObject(i);

                    Area area = new Area();

                    area.code = o1.optString("code");

                    area.name = o1.optString("name");

                    info.hotAreas.add(area);

                }

            }

            JSONArray array2 = object.optJSONArray("arealist");

            if(array2 != null){

                for (int i = 0 ; i < array2.length() ; i ++){

                    JSONObject o1 = array2.optJSONObject(i);

                    JSONArray array3 = o1.optJSONArray("list");

                    if(array3 != null && array3.length() > 0){

                        List<Area> list = new ArrayList<>();

                        info.letters.add(o1.optString("letter"));

                        for (int j = 0 ; j < array3.length() ; j ++){

                            JSONObject o2 = array3.optJSONObject(j);

                            Area area = new Area();

                            area.code = o2.optString("code");

                            area.name = o2.optString("name");

                            list.add(area);

                        }

                        info.areaMap.put(o1.optString("letter"), list);

                    }

                }

            }

            return info;

        }

        return null;

    }

    public static class Info{

        public List<Area> hotAreas = new ArrayList<>();

        public List<String> letters = new ArrayList<>();

        public Map<String, List<Area>> areaMap = new HashMap<>();

    }

    public static class Area implements Serializable{

        public String code, name;

    }

}
